//将已售出的座位标红
var allHiddenInput = $(".seats-wrapper input[type='hidden']");
console.log(allHiddenInput);
for (var i = 0; i < allHiddenInput.length; i++) {
	var soldId = allHiddenInput.eq(i).val();
	$("#"+soldId+"").attr("class","seat sold");
}

//选座购票
var allSeat = $(".seats-block .row .seat");
var selectedNum = 0; //已选座位数
var maxSelected = 5; //最大可选座位数
var sumPrice = 0; //总价格
var selectedSeatPos = new Array(); //创建一个数组来记录已选座位的坐标
for (var i = 0; i < allSeat.length; i++) {
    $(allSeat).eq(i).click(function(){
        //获取当前座位的位置坐标
        var pos = $(this).attr("seat");
        var row = pos.split("-")[0];
        var col = pos.split("-")[1];
        //处理每张票的id
        var id = row+"-"+col;
        //修改座位状态
        var status = $(this).attr("class");
        if(status.indexOf("selectable") != -1){
            if(selectedNum >= maxSelected){
                showModal("","一次最多购买"+maxSelected+"张票");
                return;
            }
            $(this).attr("class","seat selected");
            selectedNum++;
            createTicket(row,col,id);
        }else if(status.indexOf("selected") != -1){
            $(this).attr("class","seat selectable");
            selectedNum--;
            removeTicket(id);
        }
        //至少要买一张票才能提交
        if(selectedNum > 0){
            $(".confirm-order .confirm-btn").removeClass("disable");
            $(".confirm-order .confirm-btn").attr("disable",false);
        }else{
            $(".confirm-order .confirm-btn").addClass("disable");
            $(".confirm-order .confirm-btn").attr("disable",true);
        }
    });
}

//创建一条座位信息选座信息
var innerTickets = "";
function createTicket(row,col,id){
    var ticket = "<span class='ticket' id="+id+">"+row+"排"+col+"座</span>"
    //更新价格
    $("#sumPrice").html(calcPrice());
    if(selectedNum > 0){
        $(".ticket-info .no-ticket").hide();
        $(".ticket-info .has-ticket").show();
        innerTickets += ticket;
        $(".ticket-info .has-ticket .ticket-container").html(innerTickets);
        selectedSeatPos.push(id);
    }
        
}

//删除一条座位信息
function removeTicket(id){
    var ticket = $(".ticket-info .has-ticket .ticket-container").find("#"+id+"");
    ticket.remove();
    //更新价格
    $("#sumPrice").html(calcPrice());
    innerTickets = $(".ticket-info .has-ticket .ticket-container").html();
    selectedSeatPos.remove(id);
    if(selectedNum <= 0){
        $(".ticket-info .has-ticket").hide();
        $(".ticket-info .no-ticket").show();
        innerTickets = "";
        $("#sumPrice").html("0");
    }
}

//计算票价
function calcPrice(){
    var priceStr = $("#price").html();
    var price = priceStr.split("/")[0].substring(1);
    var sum = selectedNum * parseFloat(price);
    sum = sum.toFixed(1);
    return sum;
}

//显示警告框
function showModal(icon,text){
    if(icon == ""){
        $(".container .modal-container .icon").attr("class","icon");
    }else{
        $(".container .modal-container .icon").attr("class","icon " + icon);
    }
    $(".container .modal-container .tip").html(text);
    $(".container .modal-container").fadeIn(300);
}

//隐藏警告框
function hideModel(){
    $(".container .modal-container").fadeOut(300);
}

//检查选座规则
// function checkSeat(){
//     console.log(selectedSeatPos);
//     selectedSeatPos.forEach(element => {
//         var row = element.split("-")[0];
//         var col = element.split("-")[1];
//         var seat = $(".seats-block .row .seat[seat="+row+"-"+col+"]");
//         var lastNode = seat.prev(); //上一个节点
//         var lastsNode = seat.prev().prev(); //上上个节点
//         var nextNode = seat.next(); //下一个节点
//         var nextsNode = seat.next().next(); //下下个节点
//     });
// }

function contains(obj,className){
    return obj.attr("class").indexOf(className) != -1;
}

//删除数组元素
Array.prototype.remove = function(val) { 
    var index = this.indexOf(val); 
    if (index > -1) { 
        this.splice(index, 1); 
    }
};

//确认选座
$(".confirm-order .confirm-btn").click(function(){
	var selectedSeat = $(".seats-block .seat.selected");
	var showingsId = $("#showingsId").val();
	var uid = $("#uid").val();
	var seatInfo = "";
	for (var i = 0; i < selectedSeat.length; i++) {
		seatInfo += "&seatId"+i+"="+selectedSeat.eq(i).attr("id");
	}
	location.href = "createOrder.do?count="+selectedNum+"&showingsId="+showingsId+"&uid="+uid+""+seatInfo;
});















